package com.hope.androidproject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hope.androidproject.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity {

    private ViewPager mViewPager;

    //图片资源
    private List<View> mViews;

    private LinearLayout mPointLayout;

    private int[] mImageRes = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initData();
        initViews();
        initPoint();
    }

    //初始化四个点
    private void initPoint() {
        for (int i = 0; i < mImageRes.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.point_normal);
            if (i == 0) {
                imageView.setImageResource(R.drawable.point_red);
            }
            //设置点的大小
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(25, 25);
            params.setMargins(8, 0, 4, 0);
            imageView.setLayoutParams(params);
            mPointLayout.addView(imageView);
        }
    }

    //初始化数据
    private void initData() {
        mViews = new ArrayList<>();
        for (int i = 0; i < mImageRes.length; i++) {
            ImageView image = new ImageView(this);
            image.setBackgroundResource(mImageRes[i]);
            mViews.add(image);
        }
    }

    private void initViews() {
        mPointLayout = (LinearLayout) findViewById(R.id.point_container);
        mViewPager = (ViewPager) findViewById(R.id.guide_viewpager);
        mViewPager.setAdapter(new PagerAdapter() {
            public int getCount() {
                return mViews.size();
            }

            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            //实例化Item
            public Object instantiateItem(ViewGroup container, int position) {
                if (position == mImageRes.length - 1) {
                    View view = View.inflate(GuideActivity.this, R.layout.start_layout, null);
                    final Button startBtn = (Button) view.findViewById(R.id.start_btn);
                    startBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //点击开始体验按钮
                            SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                            //设为不是第一次访问
                            editor.putBoolean("isFirst",false);
                            editor.apply();
                            Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                    container.addView(view);
                    return view;
                }
                container.addView(mViews.get(position));
                return mViews.get(position);
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //页面改变之后会调用此方法
            public void onPageSelected(int position) {
                changePointState(position);
            }

            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    /**
     * 根据当前页面改变点的状态
     *
     * @param position
     */
    private void changePointState(int position) {
        //把所有的点都设置为白色    1.循环取得所有的点的    设置点的颜色
        for (int i = 0; i < mImageRes.length; i++) {
            ImageView imageview = (ImageView) mPointLayout.getChildAt(i);
            imageview.setImageResource(R.drawable.point_normal);
            //把当前点设置为红色
            if(i == position)
            {
                imageview.setImageResource(R.drawable.point_red);
            }
        }

    }
}
